﻿package com.cjh.lms.controller;

import static org.springframework.test.web.client.response.MockRestResponseCreators.withBadRequest;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.exception.LockEnterException;
import com.cjh.lms.json.JsonResult;
import com.cjh.lms.pojo.TLockInfo;
import com.cjh.lms.pojo.TLockType;
import com.cjh.lms.pojo.TUser;
import com.cjh.lms.pojo.business.AllLockInfoModel;
import com.cjh.lms.pojo.business.LockEnterModel;
import com.cjh.lms.service.LockEnterService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

@Controller
public class LockEnterController {

	@Autowired
	LockEnterService lockEnterService;

	// 锁具信息入库和产生入库记录
	@RequestMapping("addLock")
	@ResponseBody
	public JSONObject addLock(String info, String tCode, HttpServletRequest requset) {
		int count = 0;
		int num = 0;
		JSONObject jsonObject = new JSONObject();
		System.out.println(info);
		Gson gson = new Gson();
		TUser tUser = new TUser();
		tUser = (TUser)requset.getSession().getAttribute("userKey");
			String uCode = tUser.getuCode();
		List<TLockInfo> tLockInfo = gson.fromJson(info, new TypeToken<List<TLockInfo>>() {
		}.getType());
		System.out.println(tLockInfo.toString());
		try {
			num = lockEnterService.addLock(tLockInfo, tCode, uCode);
		} catch (LockEnterException e) {
			e.printStackTrace();
			System.err.println("错误的条数" + e.getCount());
			if (e.getCount() == 0) {
				count = e.getCheck();
				jsonObject.put("count", count);
				jsonObject.put("check", "10002");
				return jsonObject;
			} else {
				count = e.getCount();
			}
		}
		if (num == 0) {
			jsonObject.put("count", count);
			jsonObject.put("check", "10001");
			return jsonObject;
		}
		jsonObject.put("count", num);
		jsonObject.put("check", "00");
		return jsonObject;
	}

	// 锁具信息详情查询
	@RequestMapping("searchAllLockInfo")
	@ResponseBody
	public JsonResult searchAllLockInfo(String info, @RequestParam Integer pageSize,
			@RequestParam Integer pageCurrent) {
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		Gson gson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		AllLockInfoModel allLockInfoModel = gson.fromJson(info, AllLockInfoModel.class);
		List<AllLockInfoModel> data = lockEnterService.searchAllLockInfo(allLockInfoModel);
		PageInfo<AllLockInfoModel> pageInfo = new PageInfo<>(data);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}

	// 修改锁具信息
	@RequestMapping("updateByPrimaryKey")
	@ResponseBody
	public JSONObject updateByPrimaryKey(@RequestParam(value = "json", required = true) String json) {
		String check = null;
		String a = null;
		JSONObject jsonObject = new JSONObject();
		ObjectMapper jsonMapper = new ObjectMapper();
		Boolean err = true;
		try {
			CollectionType listType = jsonMapper.getTypeFactory().constructCollectionType(ArrayList.class,
					AllLockInfoModel.class);
			List<AllLockInfoModel> allLockInfoModels = jsonMapper.readValue(json, listType);
			if (allLockInfoModels.size() != 0)
				for (AllLockInfoModel allLockInfoModel : allLockInfoModels) {
					a = lockEnterService.updataAllLockInfo(allLockInfoModel);
					jsonObject.put("check", a);
				}
		} catch (Exception e) {
			a = "数据解析错误";
			jsonObject.put("check", a);
		}
		return jsonObject;
	}

	// 删除锁具信息
	@RequestMapping("deleteTLockInfo")
	@ResponseBody
	public JSONObject deleteTLockInfo(Integer tid) {
		JSONObject jsonObject = new JSONObject();
		jsonObject = lockEnterService.deleteLockInfo(tid);
		return jsonObject;
	}

	// 展示所有锁具型号
	@RequestMapping("selectLockType")
	@ResponseBody
	public List<TLockType> selectLockType(String info) {

		Gson gson = new Gson();
		TLockType tLockType = gson.fromJson(info, TLockType.class);
		List<TLockType> data = lockEnterService.selectLockType(tLockType);
		return data;
	}

	// 锁具型号新增
	@RequestMapping("addLockType")
	@ResponseBody
	public JSONObject addLockType(TLockType tLockType) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject = lockEnterService.addLockType(tLockType);
		} catch (LockEnterException e) {
			e.printStackTrace();
			if(e.getCheck()==10001){
				jsonObject.put("check", "10001");
				return jsonObject;
			}else{
				jsonObject.put("check", "10002");
				return jsonObject;
			}
		}
		return jsonObject;
	}

	// 入库记录查询,可以根据入库人，入库时间，锁类型查询
	@RequestMapping("searchLockEnter")
	@ResponseBody
	public JsonResult searchLockEnter(String info, @RequestParam Integer pageSize, @RequestParam Integer pageCurrent) {
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);

		Gson gson = new Gson();
		LockEnterModel lockEnterModel = gson.fromJson(info, LockEnterModel.class);
		List<LockEnterModel> data = lockEnterService.searchLockEnter(lockEnterModel);
		PageInfo<LockEnterModel> pageInfo = new PageInfo<>(data);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());

		return jsonResult;
	}

	// //入库记录删除
	// @RequestMapping("deleteLockEnter")
	// @ResponseBody
	// public String deleteLockEnter(String eCode){
	// String data = lockEnterService.deleteLockEnter(eCode);
	// return data;
	// }
}
